import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread("data/aa.png")
# 转化为灰度图
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#使用cv2.Canny边缘检测获得边缘
edges = cv2.Canny(gray,50,150,apertureSize = 3)
#使用概率霍夫变换 设置minLineLength和maxLineGap
lines = cv2.HoughLinesP(edges,1,np.pi/180,1,minLineLength = 120,maxLineGap = 10)
#为了看到霍夫变化的差异，将原图从bgr转化成 rgb，并复制一张原图片
orgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
oShow = orgb.copy()

# 对霍夫变换的结果从极坐标转换成xy坐标并在原图画线
for line in lines:
    x1,y1,x2,y2 = line[0]
    cv2.line(orgb,(x1,y1),(x2,y2),(0,0,255),2)

plt.subplot(121)
plt.imshow(oShow)
plt.axis("off")
plt.title("original")

plt.subplot(122)
plt.imshow(orgb)
plt.axis("off")
plt.title('huofu')

plt.show()